/** * @description: 全局搜索页（辅导员搜索，学生搜索） */
<template>
  <div class="pub-container">
    <div class="search-wrapper">
      <van-search v-model="value" show-action shape="round" :placeholder="$route.query.ph" @search="onSearch" @cancel="onCancel" autofocus>
        <template #action>
          <span @click="onSearch">搜索</span>
        </template>
      </van-search>
    </div>
    <div class="search-result">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh" :disabled="finished">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了..." @load="onLoad">
          <Instructor v-if="$route.query.ph === '辅导员编号/姓名'" :instructorList="instructorList" />
          <FiveHardStudent v-if="$route.query.ph === '学生学号/姓名'" :classList="classList" />
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
import { difficultStuFdy, difficultStuInfo } from '@/api/fiveHard'
import Instructor from '@/components/instructor'
import FiveHardStudent from '@/components/fiveHardStudent'
import { Toast } from 'vant'
export default {
  name: 'Search',
  components: {
    Instructor,
    FiveHardStudent,
  },
  data() {
    return {
      value: '',
      instructorList: [], //辅导员list
      classList: [], //学生list
      loading: true,
      finished: true,
      refreshing: false,
      params1: {
        //辅导员搜索条件
        collegeId: undefined,
        fdyNo: '',
        pageNum: 1,
        pageSize: 10,
      },
      params2: {
        //学生搜索条件
        stuNo: undefined,
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  methods: {
    onSearch() {
      this.finished = false
      this.params2.pageNum = 1
      if (this.$route.query.ph === '辅导员编号/姓名') {
        this.getDifficultStuFdy(this.value)
      } else if (this.$route.query.ph === '学生学号/姓名') {
        this.getDifficultStuInfo()
      }
    },

    //辅导员查询（待优化，需和学生查询共用）
    async getDifficultStuFdy(isRefresh) {
      this.params1.collegeId = this.$route.query.subId
      this.params1.fdyNo = this.value
      try {
        const { data } = await difficultStuFdy({ ...this.params1 })
        if (isRefresh) {
          this.instructorList = [...data.data.list, ...this.instructorList]
          this.refreshing = false
        } else {
          this.instructorList = [...this.instructorList, ...data.data.list]
          this.loading = false
        }
        if (data.data.list.length === 0 || data.data.list.length < 10) {
          this.finished = true
        }
      } catch (e) {
        console.log(e)
      }
    },

    //学生查询（待优化，需和辅导员查询共用）
    async getDifficultStuInfo(isRefresh) {
      try {
        this.params2.stuNo = this.value
        const { data } = await difficultStuInfo({ ...this.params2 })
        if (isRefresh) {
          this.classList = [...data.data.list, ...this.classList]
          this.refreshing = false
        } else {
          this.classList = [...this.classList, ...data.data.list]
          this.loading = false
        }
        if (data.data.list.length === 0 || data.data.list.length < 10) {
          this.finished = true
        }
      } catch (e) {
        console.log(e)
      }
    },

    // 上拉加载处理函数
    onLoad() {
      if (this.$route.query.ph === '辅导员编号/姓名') {
        this.params1.pageNum++
        this.getDifficultStuFdy()
      } else if (this.$route.query.ph === '学生学号/姓名') {
        this.params2.pageNum++
        this.getDifficultStuInfo()
      }
    },

    // 下拉刷新处理函数
    onRefresh() {
      if (this.$route.query.ph === '辅导员编号/姓名') {
        this.params1.pageNum = 1
        this.getDifficultStuFdy(true)
      } else if (this.$route.query.ph === '学生学号/姓名') {
        this.params2.pageNum = 1
        this.getDifficultStuInfo(true)
      }
    },

    onCancel() {
      Toast('取消')
    },
  },
}
</script>

<style lang="scss" scoped>
.pub-container {
  background: $bg-white;
  overflow: hidden;
}
.search-wrapper {
  padding-bottom: 15px;
}
.search-result {
  height: calc(100% - 54px);
  background: $bg-white;
  padding: 0 20px;
  overflow: hidden;
  overflow-y: auto;
  p {
    font-size: $fz16;
    padding: 10px 0;
  }
}

::v-deep {
  .van-field__control::-webkit-input-placeholder {
    color: #97979f;
    font-size: $fz14;
  }
  .van-search__content {
    background: #f5f5f5;
  }
}
</style>
